<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\logic;

use app\model\CheckIn;
use app\model\RoomLeaseRecord;
use app\model\User;
use app\model\traits\ChangeTrait;
class CheckInLogic extends RoomLeaseRecordLogic
{   
    use ChangeTrait;
    /**
     * 设置模型
     * @return string
     */
    protected function getModelName(): string
    {
        return CheckIn::class;
    }
    /**
     * @Description: 获取房间内的全部租户
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-27 17:31:42
     * @version: 1.0.0
     * @return [type]
     */
    public function Lessee($params){
        $recordWhere['room_id'] = $params['id'];
        $recordWhere['status']  = 1;
        $user_id = RoomLeaseRecord::getUserRecord($recordWhere);

        // $userWhere['id'] = ['in'=> $user_id];

        $user = User::withoutGlobalScope(['type'])->where('id','in', $user_id,)->select();

        foreach ($user as $key => $value) {
            # code...
           $user[$key]['sex'] = $this->_getSex($value['sex']);
        }

        return $user;
    }   

    /**
     * @Description: 获取全部租户信息(导出数据)
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-01 15:37:41
     * @version: 1.0.0
     * @return [type]
     */    
    public function getTenantList($params){
        
        $res = RoomLeaseRecord::getTenantList($params);
        foreach ($res as $key => $value) {
            $res[$key]['real_name'] = '';
            $res[$key]['tel']       = '';
            $res[$key]['word_unit'] = '';
            $res[$key]['roomTypeName'] = '请设置房型';
            $res[$key]['roomTypeAmount'] = '请设置房型';
            $res[$key]['roomTypePropertyPrice'] = '请设置房型';
            $res[$key]['housingName'] = '';
            $res[$key]['buildingName'] = '';
            $res[$key]['roomName'] = '';
            $res[$key]['waterElePrice'] = '暂无记录';
            $res[$key]['fee'] = '暂无记录';
            if($value['user']){
                $res[$key]['real_name'] = $value['user']['real_name'];
                $res[$key]['sex'] = $this->_getSex($value['sex']);
                $res[$key]['tel'] = $value['user']['tel'];
                $res[$key]['word_unit'] = $value['user']['word_unit'];
                $res[$key]['id_card'] = $value['user']['id_card'];
            }
            if($value['room']){
                if($value['room']['roomType']){
                    $res[$key]['roomTypeName'] = $value['room']['roomType']['name'];
                    $res[$key]['roomTypeAmount'] = $value['room']['roomType']['amount'];
                    $res[$key]['roomTypePropertyPrice'] = $value['room']['roomType']['property_price'];
                }
                $res[$key]['housingName'] = $value['room']['building']['name'];
                $res[$key]['buildingName'] =  $value['room']['building']['building_number'];
                $res[$key]['roomName'] = $value['room']['name'];
            }
            if($value['payRecord']){
                $res[$key]['waterElePrice'] = $value['payRecord']['water_price']+ $value['payRecord']['electricity_price'];
                $res[$key]['fee'] = $value['payRecord']['fee'];
            }
            // halt($value['start_time']);
            // $res[$key]['start_time'] = $value['start_time'];
        }
        return $res;
    }

    /**
     * @Description: 获取房间详情
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-02 17:34:02
     * @version: 1.0.0
     * @return [type]
     */    
    public function getRoomDetail($params){

      $where['id'] = $params['room_id'];
      $res = CheckIn::where($where)->find();

      return $res;
    }
}
